package com.activity.gaswatchermtl.dialog;

import java.util.ArrayList;

import com.activity.gaswatchermtl.R;
import com.activity.gaswatchermtl.dialog.expendablelist.ExpListAdapter;
import com.activity.gaswatchermtl.dialog.expendablelist.ExpListChild;
import com.activity.gaswatchermtl.dialog.expendablelist.ExpListGroup;
import com.app.Const;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class DialogFilters extends DialogFragment {
	private TextView distTextView_ = null;
	private int currentDist_ = 1;
	private SharedPreferences settings_ = null;
	
	private ExpandableListView expListView_ = null;
	private ArrayList<ExpListGroup> expListItems_ = null;
	private ExpListAdapter expAdapter_ = null;
	
    /* The activity that creates an instance of this dialog fragment must
     * implement this interface in order to receive event callbacks.
     * Each method passes the DialogFragment in case the host needs to query it.
     * source : http://developer.android.com/guide/topics/ui/dialogs.html */
    public interface DialogFiltersListener {
        public void onDialogPositiveClick(DialogFragment dialog);
        public void onDialogNegativeClick(DialogFragment dialog);
    }
    
    // Use this instance of the interface to deliver action events
    private DialogFiltersListener listener_;
    
    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
        	listener_ = (DialogFiltersListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }
    
	@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
                
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.filter_distance, null);
        
        settings_ = getActivity().getSharedPreferences(Const.SETTINGS_FILTERS, 0);
        currentDist_ = settings_.getInt(Const.SETTINGS_FILTERS_DISTANCE, 1);
        
        distTextView_ = (TextView) view.findViewById(R.id.filter_distance_textview_text);
        distTextView_.setText(String.valueOf(currentDist_));
        
        expListView_ = (ExpandableListView) view.findViewById(R.id.expend_list_station);
        expListItems_ = SetStandardGroups();
        expAdapter_ = new ExpListAdapter(getActivity(), expListItems_);
        expListView_.setAdapter(expAdapter_);
        expListView_.expandGroup(0);
        
        int progress = (int)(((float)currentDist_/(Const.FILTERS_DISTANCE_MAX - Const.FILTERS_DISTANCE_MIN))*100);
        SeekBar slider = (SeekBar) view.findViewById(R.id.filter_distance_seekbar);
        slider.setProgress(progress);
        
        slider.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {	
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				currentDist_ = (int)(((float)progress/100)*(Const.FILTERS_DISTANCE_MAX - Const.FILTERS_DISTANCE_MIN)+1);
				distTextView_.setText(String.valueOf(currentDist_));
			}
		});        
        
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setTitle("Change maximum distance");
        builder.setView(view)
            // Add action buttons
           .setPositiveButton("Set", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
            	   SharedPreferences.Editor editor = settings_.edit();
            	   editor.putInt(Const.SETTINGS_FILTERS_DISTANCE, currentDist_);
            	   
            	   for (String tag : Const.SETTINGS_FILTERS_STATIONS) {
                	   ExpListChild child = (ExpListChild) expAdapter_.getChildByTag(tag);
                	   if (child != null) {
                		   editor.putBoolean(tag, child.isChecked());
                	   }
            	   }
            	   
            	   ExpListChild child = (ExpListChild) expAdapter_.getChildByTag(Const.FILTERS_STATION_OTHERS);
            	   if (child != null) {
            		   editor.putBoolean(Const.FILTERS_STATION_OTHERS, child.isChecked());
            	   }
            	   
            	   
            	   editor.commit();
            	   listener_.onDialogPositiveClick(DialogFilters.this);            	   
               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
            	   listener_.onDialogNegativeClick(DialogFilters.this);
               }
           }); 

        // Create the AlertDialog object and return it
        return builder.create();
    }
	
    public ArrayList<ExpListGroup> SetStandardGroups() {
    	ArrayList<ExpListGroup> list = new ArrayList<ExpListGroup>();
    	ArrayList<ExpListChild> list2 = new ArrayList<ExpListChild>();
        
    	ExpListGroup gru1 = new ExpListGroup();
        gru1.setName("Filter by station");
        
        for (String tag : Const.SETTINGS_FILTERS_STATIONS) {
            ExpListChild child = new ExpListChild();
            child.setName(tag);
            child.setTag(tag);
            child.setChecked(settings_.getBoolean(tag, true));
            list2.add(child);
        }
        
        ExpListChild child = new ExpListChild();
        child.setName(Const.FILTERS_STATION_OTHERS);
        child.setTag(Const.FILTERS_STATION_OTHERS);
        child.setChecked(settings_.getBoolean(Const.FILTERS_STATION_OTHERS, true));
        list2.add(child);
        
        gru1.setItems(list2);
        list.add(gru1);
        
        return list;
    }
}
